package com.closeenough.app;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;

public class ProximityIntentReceiver extends BroadcastReceiver {
    
    private static final int NOTIFICATION_ID = 1000;
    private String notTitle;
    private String notDesc;
    //private boolean delOnNotif;
    private String answerEmail;
    
    //public ProximityIntentReceiver(String notificationTitle, String notificationDescription, boolean deleteOnNotification)
    public ProximityIntentReceiver(String notificationTitle, String notificationDescription, String answerEmailAddress)
    {
    	super();
    	this.notTitle = notificationTitle;
    	this.notDesc = notificationDescription;
    	this.answerEmail = answerEmailAddress;
    	//this.delOnNotif = deleteOnNotification;
    }
    
    @Override
    public void onReceive(Context context, Intent intent) {
        
//        String key = LocationManager.KEY_PROXIMITY_ENTERING;
//
//        Boolean entering = intent.getBooleanExtra(key, false);
//        
//        if (entering) {
//            Log.d(getClass().getSimpleName(), "entering");
//        }
//        else {
//            Log.d(getClass().getSimpleName(), "exiting");
//        }
        NotificationManager notificationManager = (NotificationManager)context.getSystemService(Context.NOTIFICATION_SERVICE);
        
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, null, 0);        
        
        Notification notification = createNotification();
        notification.setLatestEventInfo(context, notTitle, notDesc, pendingIntent);
        notificationManager.notify(NOTIFICATION_ID, notification);

        if(answerEmail != "")
        {
			Intent intent_send = new Intent(android.content.Intent.ACTION_SEND);
			
			intent_send.setType("application/closeenough");
							
			/* Armar mensaje.
			 */
			String subject = "I've arrived!";
			String body = "I am now in the meeting point";

			intent_send.putExtra(android.content.Intent.EXTRA_EMAIL, new String[]{ answerEmail });                            
			intent_send.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
			intent_send.putExtra(android.content.Intent.EXTRA_TEXT, body);
			
			context.startActivity(intent_send);
        }
//        if(delOnNotif)
//        {
//        	
//        }
    }
    
    private Notification createNotification() {
        Notification notification = new Notification();
        
        notification.icon = R.drawable.androidmarker;
        notification.when = System.currentTimeMillis();
        
        notification.flags |= Notification.FLAG_AUTO_CANCEL;
        notification.flags |= Notification.FLAG_SHOW_LIGHTS;
        
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_LIGHTS;
        
        notification.ledARGB = Color.WHITE;
        notification.ledOnMS = 1500;
        notification.ledOffMS = 1500;
        
        return notification;
    }
}